<?php
// +----------------------------------------------------------------------
// | 中犇单商户
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2021 中犇科技 All rights reserved.
// +----------------------------------------------------------------------


namespace app\admin\admin;

use app\admin\model\UploadCategory;
use app\common\controller\Common;
use app\admin\model\Upload as UploadModel;

/**
 * 后台公共控制器
 * @package app\admin\admin
 */
class Upload extends Common
{
    
    use \app\common\traits\controller\Upload;//继承中间件
	/**
     * 保存文件
     * @param string $dir 附件存放的目录
     * @param string $from 来源
     * @param string $module 来自哪个模块
     * @author 刘明美 [ liumingmei@zhongbenjituan.com ]
     * @return string|\think\response\Json
     */
    public function save($dir = '', $from = '', $module = 'admin' , $returnarray = false)
    {
        
        set_time_limit(0);
        if ($dir == '') $this->error(lang('没有指定上传目录'));
        if ($from == 'ueditor') return $this->ueditor();
        if ($from == 'jcrop') return $this->jcrop();
        return $this->saveFile($dir, $from, $module);
    }
    
    public function get_ueditor(){
        return $this->ueditor();
    }

    /**
     * 检查附件是否存在
     * @param string $md5 文件md5
     * @author 刘明美 [ liumingmei@zhongbenjituan.com ]
     * @return \think\response\Json
     */
    public function check($md5 = '')
    {
        $md5 == '' && $this->error(lang('参数错误'));

        // 判断附件是否已存在
        if ($file_exists = UploadModel::get(['md5' => $md5])) {
            $file_exists->category_id = $this->request->post('category_id', 0);
            $file_exists->save();
            $file_path = $file_exists->path;
            return json([
                'code'   => 1,
                'info'   => lang('上传成功'),
                'class'  => 'success',
                'id'     => $file_exists->id,
                'path'   => $file_path
            ]);
        } else {
            $this->error(lang('文件不存在'));
        }
        $this->error(lang('文件不存在'));

    }

    /**
     * 保存列表
     * php迷途小书童
     * 2022/6/7 9:00 下午
     */
    public function category_save()
    {
        $data = $this->request->post();
        // 验证
        $result = $this->validate($data, 'UploadCategory.save');
        // 验证失败 输出错误信息
        if (true !== $result) $this->error($result);
        if (isset($data['id']) && $data['id']) {
            UploadCategory::update($data);
        } else {
            UploadCategory::create($data);
        }
        return json(['code' => 1,'msg' => '保存成功']);
    }
    /**
     * 删除分类
     * name
     * php迷途小书童
     * 2022/6/7 9:00 下午
     */
    public function category_delete()
    {
        $data = $this->request->post();
        // 验证
        $result = $this->validate($data, 'UploadCategory.delete');
        // 验证失败 输出错误信息
        if (true !== $result) $this->error($result);
        UploadCategory::where('id', $data['id'])->delete();
        return json(['code' => 1,'msg' => '删除成功']);
    }
    /**
     * 分类列表
     * php迷途小书童
     * 2022/6/7 9:00 下午
     */
    public function category_list()
    {
        $categories = UploadCategory::where('status',1)->select();
        return json(['code' => 1,'data' => $categories]);
    }
    /**
     * 图片列表
     * php迷途小书童
     * 2022/6/7 9:00 下午
     */
    public function image_list()
    {
        $data = $this->request->post();
        $where = [];
        if (isset($data['category_id']) && $data['category_id']) {
            $where[] = ['category_id', 'eq', $data['category_id']];
        }
        $images = \app\admin\model\Upload::where('status', 1)
            ->field('id,path')
            ->where($where)
            ->paginate();
        return json(['code' => 1, 'data' => $images]);
    }
    /**
     * 图片删除
     * php迷途小书童
     * 2022/6/7 9:00 下午
     */
    public function image_del()
    {
        $data = $this->request->post();
        $where = [];
        if (isset($data['ids']) && $data['ids']) {
            $where[] = ['id', 'in', $data['ids']];
        }
        \app\admin\model\Upload::where($where)
            ->delete();
        return json(['code' => 1, 'msg' => '删除成功']);
    }

    /**
     * 图册页面
     * php迷途小书童
     * 2022/6/15 10:39 上午
     * @return mixed
     */
    public function atlas()
    {
        return $this->fetch();
    }
   
}